package com.wyk.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.netty.util.Timeout;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**流向限制接口
 * @author WangYuKai
 * @Date 2022/7/27
 * @apiNote
 */
@RestController
public class FlowLimitController {

    @GetMapping("/testA")
    public String TestA(){
        //休息0.8秒钟
        try {
            TimeUnit.MILLISECONDS.sleep(800);
        }catch (InterruptedException e){e.printStackTrace();};
        return "testA------------";
    }

    @GetMapping("/testB")
    public String TestB(){
        return "testB------------";
    }

    @GetMapping("/testC")
    public String TestC(){
        System.out.println("测试服务熔断的RT模式");
        try {//暂停线程一秒钟
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e){e.printStackTrace();};
        return "testC------------";
    }

    @GetMapping("/testD")
    public String TestD(){
        System.out.println("测试服务熔断的异常比例模式");
        int age = 10 / 0;
        return "testC------------";
    }

    @GetMapping("/getHotKey")
    @SentinelResource(value = "getHotKey",blockHandler = "deal_getHotKey")
    public String getHotKey(@RequestParam(value = "p1",required = false) String p1,
                            @RequestParam(value = "p2",required = false) String p2){
        return "-------测试热点key的限流机制";
    }

    public String deal_getHotKey(String p1, String p2, BlockException blockException){
     return "热点key的兜底方案.....";
    }
}
